package com.xmall.service.impl;

import com.xmall.bean.table.Category;
import com.xmall.bean.table.CategoryExample;
import com.xmall.bean.vo.CategoryVO;
import com.xmall.mapper.CategoryMapper;
import com.xmall.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Map<String, Object> showCurrentCatalog(Integer id) {
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        List<CategoryVO> currentSubCategory = categoryMapper.selectByPid(id);
        Map<String,Object> map = new HashMap<>();
        map.put("currentCategory",currentCategory);
        map.put("currentSubCategory",currentSubCategory);
        return map;
    }

    @Override
    public Map<String, Object> showIndexInCatalog() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        String level = "L1";
        if(level != null){
            criteria.andLevelEqualTo(level);
        }
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        Map<String,Object> map = new HashMap<>();
        map.put("categoryList",categories);
        Category category = categories.get(0);
        Category currentCategory = categoryMapper.selectByPrimaryKey(category.getId());
        List<CategoryVO> currentSubCategory = categoryMapper.selectByPid(category.getPid());
        map.put("currentCategory",currentCategory);
        map.put("currentSubCategory",currentSubCategory);
        return map;
    }
}
